package com.java.util;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * @desc
 * @author zhengzy
 * @version 2016年5月21日
 */
public class PwdUtil {
	/**
	 * 加密
	 * 
	 * @param key
	 * @param text
	 * @return
	 * @throws Exception 
	 */
	public static String encrypt(String key, String text) {
		// String key = DESCoder.initKey();
		if (key == null) {
			key = "rg4Zkg5ej08=";
		}
		byte[] inputData = text.getBytes();
		try {
			inputData = DESCoder.encrypt(inputData, key);
			return DESCoder.encryptBASE64(inputData);
		} catch (Exception e) {
			return null;
		}

	}

	/**
	 * 解密
	 * 
	 * @param key
	 * @param text
	 * @return
	 * @throws Exception 
	 */
	public static String decode(String key, String text) {
		if (key == null) {
			key = "rg4Zkg5ej08=";
		}
		byte[] outputData;
		String outputStr = null;
		try {
			outputData = DESCoder.decrypt(DESCoder.decryptBASE64(text), key);
			outputStr = new String(outputData);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return outputStr;
	}
	public static String encryptMd5(String text){
		return DigestUtils.md5Hex(text);
		
	}
	
	public static void main(String[] args) throws Exception {
		String in = PwdUtil.encrypt(null, "123");
		System.out.println(in);
		System.out.println(PwdUtil.decode(null, in));
		
		System.out.println(PwdUtil.encryptMd5("123"));
		System.out.println(PwdUtil.encryptMd5("1234"));
		System.out.println(PwdUtil.encryptMd5("1235"));
	}
}
